package servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import usecases.GestionApprentiPizzaiolo;
import domaine.ApprentiPizzaiolo;

/**
 * Servlet implementation class Login
 */
@WebServlet(name="seconnecter.html")
public class SeConnecter extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB
	private GestionApprentiPizzaiolo gestionApprentiPizzaiolo;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		String login = request.getParameter("login");

		String mdp = request.getParameter("mdp");
		session = request.getSession(true);
		try {
			ApprentiPizzaiolo apprenti = gestionApprentiPizzaiolo
					.getApprentiPizzaiolo(login, mdp);
			synchronized (session) {
				session.setAttribute("apprenti", apprenti);
			}
			//pour rajouter la session si le navigateur n'accepte pas les cookies
			getServletContext().getNamedDispatcher("salledattente.html").forward(request, response);
		} catch (Throwable e) {
			getServletContext().getNamedDispatcher("index.html").forward(request, response);
		}
		
	}

}
